﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using PoolGame2.Controls;
using PoolGame2.Game;

namespace PoolGame2.Managers
{
    public class CameraManager : IGameControlListener
    {
        protected List<Camera> cameras;
        protected List<Camera> autoManagedCameras;
        protected int activeCamera;
        protected bool automanage = false;

        public CameraManager(PoolGame game)
        {
            cameras = new List<Camera>();
            autoManagedCameras = new List<Camera>();
            FixedCamera cam = new FixedCamera(game, new Vector3(0, 155, 0), Vector3.Zero, Vector3.Forward);
            cam.UpdateProjection(0.01f, 1000f, 60);
            AddCamera(cam, false);
            AddCamera(new FixedCamera(game, new Vector3(-100, 200, 0), Vector3.Zero, Vector3.Up), false);
            AddCamera(new FixedCamera(game, new Vector3(100, 200, 0), Vector3.Zero, Vector3.Up), false);
            AddCamera(new FreeCamera(game, new Vector3(0, 120.0f, 155.0f), Vector3.Zero, Vector3.Up), true);

            activeCamera = 0;
        }

        public Camera GetCamera()
        {
            if (automanage && autoManagedCameras.Count != 0)
            {
                return GetDirectedCamera();
            } else {
                return cameras[activeCamera];
            }
        }

        protected Camera GetDirectedCamera()
        {
            return autoManagedCameras[0];
        }

        public int GetActiveCamera()
        {
            return activeCamera;
        }

        public void NextCamera()
        {
            if (!automanage)
            {
                activeCamera++;
                if (activeCamera >= cameras.Count)
                {
                    activeCamera = 0;
                }
                SetCameraActive(activeCamera);
            }
        }

        public void PreviousCamera()
        {
            if (!automanage)
            {
                activeCamera--;
                if (activeCamera < 0)
                {
                    activeCamera = cameras.Count - 1;
                }
                SetCameraActive(activeCamera);
            }
        }

        public void SetCameraActive(int camNumber)
        {
            int count = 0;
            foreach (Camera camera in cameras)
            {
                if (count == camNumber)
                {
                    cameras[count].setActive(true);
                }
                else
                {
                    cameras[count].setActive(false);
                }
                count++;
            }
        }

        public void AddCamera(Camera camera, bool registerForControls)
        {
            if (registerForControls)
            {
                PoolGame.gameControls.addListener(camera);
            }
            cameras.Add(camera);
        }

        public void AddAutoManagedCamera(Camera camera)
        {
            autoManagedCameras.Add(camera);
        }

        public void GameControlPerformed(List<GameControlAction> actions)
        {
            foreach (GameControlAction action in actions)
            {
                ControlActions a = action.GetAction();
                if (a == ControlActions.CAMERA_CYCLE_UP)
                {
                    NextCamera();
                    setAutoMangage(false);
                }
                else if (a == ControlActions.CAMERA_CYCLE_DOWN)
                {
                    PreviousCamera();
                    setAutoMangage(false);
                }
            }
        }

        public void setAutoMangage(bool a)
        {
            automanage = a;
        }

        public Vector2 ConvertToScreenSpace(Vector3 pos)
        {
            return GetCamera().ConvertToScreenSpace(pos);
        }

    }
}
